using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DM.DAO;
using DM.Entity;

namespace DM.Web.Admin.QuanLySanPham
{
    public partial class QuanLySanPham : System.Web.UI.Page
    {
        readonly TinTucDAO tinTucService = new TinTucDAO();      

        protected void Page_Load(object sender, EventArgs e)
        {
            BindingGridView();
        }

        private void BindingGridView()
        {
            DataSet dataSet = tinTucService.GetTinTucByMaLoaiTinVaNgonNgu(32, Common.ParseInt32(DropDownListNgonNgu.SelectedValue));
            DataView dataView = dataSet.Tables[0].DefaultView;
            dataView.Sort = ShortColumn + " " + (ShortAscend ? "ASC" : "DESC");
            GridViewSanPham.DataSource = dataView;
            GridViewSanPham.DataBind();
        }

        /// <summary>
        /// Property Column nào shorting trong GridViewTinTuc
        /// </summary>
        private string ShortColumn
        {
            get
            {
                object o = ViewState["ShortColumn"];
                if (o == null) return "NgayDang";
                return (string)o;
            }
            set { ViewState["ShortColumn"] = value; }
        }

        /// <summary>
        /// Property Short theo hướng nào (Ascend hay Descend)
        /// </summary>
        private bool ShortAscend
        {
            get
            {
                object o = ViewState["ShortAscend"];
                if (o == null) return false;
                return (bool)o;
            }
            set { ViewState["ShortAscend"] = value; }
        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemMoiSanPham.aspx");
        }

        protected void GridViewSanPham_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (e.SortExpression == ShortColumn) ShortAscend = !ShortAscend;
            else
            {
                ShortColumn = e.SortExpression;
                ShortAscend = true;
            }
            BindingGridView();
        }

        protected void GridViewSanPham_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewSanPham.EditIndex = -1;
        }

        protected void GridViewSanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewSanPham.PageIndex = e.NewPageIndex;
            BindingGridView();
        }

        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            Button buttonEdit = (Button)sender;
            GridViewRow rowEdit = (GridViewRow)buttonEdit.Parent.Parent;
            string maTin = GridViewSanPham.DataKeys[rowEdit.RowIndex].Value.ToString();
            Response.Redirect("ThemMoiSanPham.aspx?MaTin=" + maTin);
        }

        protected void ButtonXoa_Click(object sender, EventArgs e)
        {
            Button buttonXoa = (Button)sender;
            GridViewRow currentRow = (GridViewRow)buttonXoa.Parent.Parent;
            GridViewSanPham.EditIndex = currentRow.RowIndex;
            GridViewSanPham.DataBind();
        }

        protected void ButtonDel_Click(object sender, EventArgs e)
        {
            Button ButtonOk = (Button)sender;
            GridViewRow deleteRow = (GridViewRow)ButtonOk.Parent.Parent;
            string maTin = GridViewSanPham.DataKeys[deleteRow.RowIndex].Value.ToString();
            int i = GridViewSanPham.PageIndex;
            GridViewSanPham.DeleteRow(deleteRow.RowIndex);
            tinTucService.Delete(maTin);
            GridViewSanPham.DataBind();
            Common.Show("Tin đã bị xóa!");
            Response.Redirect("DanhSachSanPham.aspx");
        }

        protected void ButtonNoDel_Click(object sender, EventArgs e)
        {
            GridViewSanPham.EditIndex = -1;
            GridViewSanPham.DataBind();
        }
    }
}
